﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;

namespace PersonalWebSite.Common
{
    /// <summary>
    /// Defines the Navigation Manager
    /// </summary>
    public class NavigationManager
    {
        #region Variables
        private readonly HttpSessionStateBase session = null;
        protected const string sessionKeyNavigationHistory = "NavigationManager_NavigationHistory";
        #endregion

        #region Properties
        /// <summary>
        /// Gets the Navigation History
        /// </summary>
        protected List<RouteData> NavigationHistory
        {
            get
            {
                if (session[sessionKeyNavigationHistory] == null)
                    session[sessionKeyNavigationHistory] = new List<RouteData>();

                return session[sessionKeyNavigationHistory] as List<RouteData>;
            }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Creates a new instance of this type
        /// </summary>
        /// <param name="session">session</param>
        public NavigationManager(HttpSessionStateBase session)
        {
            this.session = session;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Add a Navigation History Item
        /// </summary>
        /// <param name="routeData">Item to add</param>
        public void AddNavigationHistoryItem(RouteData routeData)
        {
            NavigationHistory.Add(routeData);
        }

        /// <summary>
        /// Fetches the previous history item, to be used for Back functionality
        /// </summary>
        /// <returns>previous history item, to be used for Back functionality</returns>
        public RouteData FetchPreviousHistoryItem()
        {
            var navigationHistory = NavigationHistory;

            return navigationHistory.Count == 0 ? null : navigationHistory[navigationHistory.Count - 1];
        }
        #endregion
    }
}